package com.hup.utils.io.socket.udp;

import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.net.InetAddress;

/**
 * @author hugan
 * @date 2020/3/29
 */
@Log4j2
public class UdpClient extends UdpCore {

    @Setter
    private InetAddress serverAddress;
    @Setter
    private int serverPort;

    public UdpClient(InetAddress serverAddress, int serverPort, UdpHandler handler) {
        this(serverAddress, serverPort, null, handler);
    }

    public UdpClient(InetAddress serverAddress, int serverPort, Integer clientPort, UdpHandler handler) {
        super(clientPort, handler);
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;
    }

    @Override
    protected ThreadPoolTaskExecutor initExecutor() {
        ThreadPoolTaskExecutor executor = super.initExecutor();
        executor.setThreadNamePrefix("udp-client-");
        return executor;
    }

    /**
     * 给服务端发送消息,异步的
     */
    public void send(byte[] data, SendDataHandler handler) {
        super.send(data, serverAddress, serverPort, handler);
    }

}
